<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    zaki<>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_type.class.php 2011-07-06 22:27:59 $
 * @Show Note: 
 */
Class zy_type extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = PAGENO;
		
		/**
		 *	Construct this module class.
		*/
		public function zy_type()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="zy_type.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("zy_type");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->zy_type();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
				echo $col = "mc_type_id";
				$where = 1;
				
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				$error_sql = end($this->db->getDebugMsg());
				echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				
				$this->smarty->assign('arr',$arr);
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
			$conf_item_area = get_custom_conf('conf_item_area'); //所属区域

			//添加数据
			if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
			{
				//使用模版，展示提交界面
				$this->smarty->assign('act_title','添加');
				$this->smarty->assign('act','add');
				$this->smarty->assign('conf_item_area', $conf_item_area);
				$this->smarty->display($this->tplFile);
			} else {
				//用户提交，把数据插入数据库
				$temp = $this->getValue();
				//dump($temp);
				$insrt = $this->db->insert_zy_type($temp);
				//$error_no = 0 > $insrt ?  $insrt :'-1501';
				admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				//alertMsg($error_no, 'index.php?c=zy_type');
				if(0 < $insrt) { //成功直接跳转
					redirectPage('index.php?c=zy_type');
				} else {
					alertMsg($error_no, 'index.php?c=zy_type');
				}			
			}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
			$conf_item_area = get_custom_conf('conf_item_area'); //所属区域

				//取得id，也就是唯一索引
				$mc_type_id = isset($_REQUEST['mc_type_id']) ? intval(trim($_REQUEST['mc_type_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($mc_type_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=zy_type');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->assign('conf_item_area', $conf_item_area);
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "mc_type_id=$mc_type_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_zy_type($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						if ($update < 0) {
							alertMsg($error_no, 'index.php?c=zy_type');
						}else{
							redirectPage('index.php?c=zy_type');
						}
				}
		}
		/**
	     *	browse method.
	     */
		public function browse() {
			$mc_type_id = isset($_REQUEST['mc_type_id']) ? intval(trim($_REQUEST['mc_type_id'])) : 0 ;
			//使用模版，展示提交界面
			$result = $this->db->getOne($mc_type_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_type');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			//$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','查看');
			$this->smarty->assign('act','browse');
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$mc_type_id = isset($_REQUEST['mc_type_id']) ? intval(trim($_REQUEST['mc_type_id'])) : 0 ;
				//查看设备表中是否存在该项目的ID 如果存在提示后退出
				$temp = $this->check_project_is_exist($mc_type_id);
				if (!empty($temp)) {
					alertMsg('-2006');
				}
				$where = "mc_type_id=$mc_type_id";
				$del=$this->db->delete_zy_type($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if ($del < 0) {
					alertMsg($error_no, 'index.php?c=zy_type');
				}else{
					redirectPage('index.php?c=zy_type');
				}
		}
		
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			//补充其他的字段
			$temp['project_name'] = isset($_POST['project_name']) ? htmlspecialchars(trim($_POST['project_name'])) : '';//项目名称
			if (empty($temp['project_name'])) {
				alertMsg('-2080');
			}
			if($isEdit){
				$temp['mc_type_id'] = isset($_POST['mc_type_id']) ? intval($_POST['mc_type_id']) : 0;
				$is_exist = $this->check_project_name_exist($temp['project_name'],$temp['mc_type_id']);
				if ($is_exist) {
					alertMsg('-2081');
				}
			} else {
				$temp['mc_type_id'] = "";
				$is_exist = $this->check_project_name_exist($temp['project_name']);
				if ($is_exist) {
					alertMsg('-2081');
				}
			}
			
			$temp['project_manager'] = isset($_POST['project_manager']) ? htmlspecialchars(trim($_POST['project_manager'])) : '';//项目经理
			$temp['technical_manager'] = isset($_POST['technical_manager']) ? htmlspecialchars(trim($_POST['technical_manager'])) : '';//技术经理
			$temp['operation_manager'] = isset($_POST['operation_manager']) ? htmlspecialchars(trim($_POST['operation_manager'])) : '';//作业经理
			$temp['area_belongs'] = isset($_POST['area_belongs']) ? htmlspecialchars(trim($_POST['area_belongs'])) : '';//所属区域
			$temp['contact_way'] = isset($_POST['contact_way']) ? htmlspecialchars(trim($_POST['contact_way'])) : '';//联系方式
			//重复验证
			
			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'项目表管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'browse' => array('browse', '查看', 16, false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}
		/**
		 * 检测项目ID是否存在虚拟设备表中
		 *
		 * @param unknown_type $mc_type_id
		 * @return 0代表不存在  1代表存在
		 */
		function check_project_is_exist($mc_type_id)
		{
			$zy_mc_type_db = Factory::getDB('zy_mc_type');
			return $zy_mc_type_db->check_project_is_exist($mc_type_id);
		}
		/**
		 * 检测项目名称是否重复
		 *
		 * @param unknown_type $project_name
		 * @param unknown_type $mc_type_id
		 * @return unknown
		 */
		function check_project_name_exist($project_name,$mc_type_id='')
		{
			$where = "project_name = '{$project_name}' ";
			if (!empty($mc_type_id)) {
				$where .= " AND mc_type_id != {$mc_type_id} " ;
			}
			$temp = $this->db->getAll($where,'mc_type_id',1,0);
			return isset($temp[0]) ? 1  : 0;
 		}
}

?>